<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;
use Album\Form\AlbumFilter;

class AlbumController extends AbstractActionController
{
    protected $albumTable;

    public function indexAction()
    {                  
        return new ViewModel(array('albums' => $this->getAlbumTable()->fetchAll()));
    }
    
    public function addAction()
    {
        $form = new AlbumForm();
        $form->get('submit')->setValue('Add');
        $request = $this->getRequest(); 
        if($request->isPost())
        {
           
            $album = new Album(); 
            $albumFilter = $album->getInputFilter();//new AlbumFilter();//$album->getInputFilter();   
           
            $form->setInputFilter($albumFilter); 
            $form->setData($request->getPost());
            if($form->isValid())
            {
                $album->exchageArray($form->getData());
                $this->getAlbumTable()->saveAlbum($album);
                //redirect to list of album
                return $this->redirect()->toRoute('album');
            }
            else
            {
                $model = new ViewModel(array('error' => true , 'form' => $form));
                $model->setTemplate('album/album/add');
                return $model;
            }
        }
        return array('form' => $form);
    }
    
    public function editAction()
    {
        $id =  (int)$this->params()->fromRoute('id' , 0);
        if(!$id)
        {
            return $this->redirect()->toRoute('album' , array('action' => 'add'));
        }
        try{
            $album = $this->getAlbumTable()->getAlbum($id);
        } catch (Exception $ex) {
            return $this->redirect()->toRoute('album' , array('action' => 'index'));
        }
        
        $form = new AlbumForm(); 
        $form->bind($album);
        $form->get('submit')->setAttribute('value', 'edit');
        $request = $this->getRequest();
        if($request->isPost())
        { 
            $albumFilter = new AlbumFilter();//$album->getInputFilter();  
            $form->setInputFilter($albumFilter);
            $form->setData($request->getPost()); 
            if($form->isValid())
            {  
                $this->getAlbumTable()->saveAlbum($album); 
                return $this->redirect()->toRoute('album');
            }
        }
        return array(
            'id' => $id ,
            'form' => $form
        );
    }
    
    public function deleteAction()
    {
        $id = $this->params()->fromRoute('id' , 0);
        if(!id)
        {
            return $this->redirect()->toRoute('album');
        }
        $request = $this->getRequest();
        if($request->isPost())
        {
            $del = $request->getPost('del' , 'No');
            if($del == "Yes")
            {
                $id = (int)$request->getPost('id');
                $this->getAlbumTable()->deleteAlbum($id);
            }
            return $this->redirect()->toRoute('album');
        }
        return array(
            'id' => $id ,
            'album' => $this->getAlbumTable()->getAlbum($id)
        );
    }
    
    public function getAlbumTable()
    {
        if(!$this->albumTable)
        {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');     
        }
        return $this->albumTable;
    }
}